package com.jason.www.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.BaseRequestOptions;
import com.jason.www.R;
import com.jason.www.base.RunApplication;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import androidx.annotation.NonNull;

public class GlideUtils {
    private static Context context;
    private static Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 101:
                    HashMap<String, ImageView> map = (HashMap<String, ImageView>) msg.obj;
                    for (String imgUrl : map.keySet()) {
                        RequestBuilder<Drawable> requestBuilder = load(imgUrl);
                        setConfiguration(requestBuilder);
                        bind2View(requestBuilder, map.get(imgUrl));
                    }
                    break;
            }
        }
    };

    public static void init(Context mContext) {
        context = mContext;
    }

    private static RequestManager with() {
        return Glide.with(context);
    }

    private static RequestBuilder<Drawable> load(String imgUrl) {
        return with().load(imgUrl);
    }

    private static RequestBuilder<Drawable> load(int resId) {
        return with().load(resId);
    }

    private static RequestBuilder<Drawable> load(File file) {
        return with().load(file);
    }

    private static BaseRequestOptions setConfiguration(RequestBuilder request) {
        return request.diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .error(R.drawable.default_pic)
                .placeholder(R.drawable.default_pic)
                .centerCrop();
    }

    private static BaseRequestOptions setAvatarConfiguration(RequestBuilder request) {
        BaseRequestOptions options = setConfiguration(request);
        return options.error(R.drawable.default_avatar);
    }

    public static void pauseLoad() {
        with().pauseRequests();
    }

    public static void resumeLoad() {
        with().resumeRequests();
    }

    public static void loadAvatar(ImageView imageView, String imgUrl) {
        RequestBuilder<Drawable> requestBuilder = load(imgUrl);
        setAvatarConfiguration(requestBuilder);
        bind2View(requestBuilder, imageView);
    }

    public static void loadImage(ImageView imageView, String imgUrl) {
        if (TextUtils.equals(Thread.currentThread().getName(), "main")) {
            RequestBuilder<Drawable> requestBuilder = load(imgUrl);
            setConfiguration(requestBuilder);
            bind2View(requestBuilder, imageView);
        } else {
            Message message = Message.obtain();
            message.what = 101;
            HashMap<String, ImageView> map = new HashMap<>();
            map.put(imgUrl, imageView);
            message.obj = map;
            handler.sendMessage(message);
        }
    }

    public static void loadImage(ImageView imageView, int resId) {
        RequestBuilder<Drawable> requestBuilder = load(resId);
        setConfiguration(requestBuilder);
        bind2View(requestBuilder, imageView);
    }

    public static void loadImage(ImageView imageView, File file) {
        RequestBuilder<Drawable> requestBuilder = load(file);
        setConfiguration(requestBuilder);
        bind2View(requestBuilder, imageView);
    }

    private static void bind2View(RequestBuilder<Drawable> requestBuilder, ImageView imageView) {
        if (TextUtils.equals(Thread.currentThread().getName(), "main")) {
            requestBuilder.into(imageView);
        }
    }

    public static void loadLocalImage(ImageView imageView, String path) {
        loadImage(imageView, new File(path));
    }

    public static Bitmap getBitmapFromUrl(String url) throws ExecutionException, InterruptedException {
        int width = DisplayUtils.dp2px(30);
        return Glide.with(RunApplication.getContext())
                .asBitmap() //必须
                .load(url)
                .centerCrop()
                .into(width, width)
                .get();
    }
}
